package com.yc.jpyy.file.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.file.service.FileService;
import com.yc.jpyy.utils.Tools;

@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(FileController.class);

	@Autowired
	private FileService fileService;
	
	/**
	 * bese64文件上传
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/uploadFile", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String uploadFile(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			/*//获取流
			InputStream is = request.getInputStream();  
			byte[] bytes = new byte[1024 * 1024]; 
			int nRead = 1;  
			int nTotalRead = 0;  
			while (nRead > 0) {  
			    nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);  
			    if (nRead > 0)  
			        nTotalRead = nTotalRead + nRead;  
			} 
			//读取的字节流转换成字符串
			String fileInfo = new String(bytes, 0, nTotalRead, "utf-8");  */
			String fileInfo = request.getAttribute("info").toString();
			Map<String,Object> paramsMap = Tools.JsonToMap(fileInfo);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = fileService.uploadFile(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("uploadFile" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "上传失败");
		} finally {
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * bse64多文件上传
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/uploadFiles", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String uploadFiles(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			/*//String fileInfo = request.getParameter("Info");
			//获取流
			InputStream is = request.getInputStream();   
			
			byte[] bytes = new byte[request.getContentLength()]; 
			int nRead = 1;  
			int nTotalRead = 0;  
			while (nRead > 0) {  
			    nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);  
			    if (nRead > 0)  
			        nTotalRead = nTotalRead + nRead;  
			} 
			//读取的字节流转换成字符串
			String fileInfo = new String(bytes, 0, nTotalRead, "utf-8"); */
			String fileInfo = request.getAttribute("info").toString();
			Map<String,Object> paramsMap = Tools.JsonToMap(fileInfo);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = fileService.uploadFiles(fileInfo);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("uploadFiles" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "上传失败");
		} finally {
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 二进制文件上传
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/uploadFileByte", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String uploadFileByte(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			/*//获取流
			InputStream is = request.getInputStream();  
			byte[] bytes = new byte[1024 * 1024]; 
			int nRead = 1;  
			int nTotalRead = 0;  
			while (nRead > 0) {  
			    nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);  
			    if (nRead > 0)  
			        nTotalRead = nTotalRead + nRead;  
			} 
			//读取的字节流转换成字符串
			String fileInfo = new String(bytes, 0, nTotalRead, "utf-8"); */ 
			String fileInfo = request.getAttribute("info").toString();
			Map<String,Object> paramsMap = Tools.JsonToMap(fileInfo);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = fileService.uploadFileByte(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("uploadFileByte" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "上传失败");
		} finally {
			return JSON.toJSONString(resultMap);
		}
	}
	
	
	/**
	 * 文件表新增
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addFileManagerInfo", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addFileManagerInfo(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			String fileInfo = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(fileInfo);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = fileService.addFile(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("addFileManagerInfo" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "新增失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 根据文件名称查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetFilesInfo", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String GetFilesInfo(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			String queryParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = fileService.queryFileByName(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("GetFilesInfo" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}

	/**
	 * 根据文件名称删除
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/delFileManagerInfo", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String delFileManagerInfo(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			String deleteParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(deleteParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = fileService.deleteFile(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("delFileManagerInfo" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "删除失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 修改附件信息 
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/UpdateAnnex", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String UpdateAnnex(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			String updateParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(updateParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = fileService.updateAnnex(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("UpdateAnnex" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "删除失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}

}
